package com.idopartx.phonelightning.widget.screen

import kotlin.jvm.JvmOverloads
import android.animation.ValueAnimator.AnimatorUpdateListener
import com.idopartx.phonelightning.widget.KBubbleUtils
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import java.util.ArrayList

class BreathView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    var speed: Int = 1
        set(value) {
            field = value
            if (mAnimator?.isRunning == true) {
                mAnimator?.cancel()
                mAnimator?.duration = durrarion.toLong() - value * 400
                mAnimator?.start()
            }
            invalidate()
        }

    var breathWidth: Int = 100
        set(value) {
            field = 100 + value * 10
            if (mAnimator?.isRunning == true) {
                mAnimator?.cancel()
                mAnimator?.setIntValues(*getIntArray())
                mAnimator?.start()
            }
            invalidate()
        }

    /**
     * 扩散圆圈颜色
     */
    private var mDiffusionColor = Color.parseColor("#F32400")

    /**
     * 整个绘制面的最大宽度
     */
    private val mMaxWidth = com.idopartx.phonelightning.widget.KBubbleUtils.dp2px(0.5f).toFloat()

    /***
     * 初始透明度
     */
    private val alpha = 255

    private var mPaint: Paint? = null
    private var mFraction = 0 //变化因子
    private var mAnimator: ValueAnimator? = null
    private var durrarion = 5000 //闪烁持续时间

    init {
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint!!.style = Paint.Style.STROKE
        //        mPaint.setMaskFilter(new BlurMaskFilter(mMaxWidth, BlurMaskFilter.Blur.INNER));
        mPaint!!.strokeWidth = mMaxWidth
        mPaint!!.color = Color.parseColor("#ff5000")

        mAnimator = ValueAnimator.ofInt(*getIntArray())
        mAnimator?.duration = durrarion.toLong()
        mAnimator?.repeatCount = ValueAnimator.INFINITE
        mAnimator?.addUpdateListener {
            mFraction = it.animatedValue as Int
            invalidate()
        }
        mAnimator?.start()
    }

    private fun getIntArray(): IntArray {
        val list: MutableList<Int> = ArrayList()
        for (j in 0 until breathWidth / 2) {
            list.add(j)
        }
        for (k in breathWidth / 2 downTo 1) {
            list.add(k)
        }
        val ints = IntArray(list.size)
        for (i in list.indices) {
            ints[i] = list[i]
        }
        return ints
    }

    override fun onDraw(canvas: Canvas) {
        mPaint!!.color = mDiffusionColor
        super.onDraw(canvas)
        for (i in 0 until mFraction) {
            mPaint?.alpha = alpha - (alpha * i / mFraction)
            canvas.drawRect(
                i * mMaxWidth,
                i * mMaxWidth,
                width - i * mMaxWidth,
                height - i * mMaxWidth, mPaint!!
            )

        }
    }


    fun setColor(color: Int) {
        mDiffusionColor = color
        invalidate()
    }
}